<?php

namespace App\Http\Controllers\Author;

use App\Common\Result;
use App\Http\Controllers\Controller;
use App\Service\Author\AuthorService;
use App\Service\Customer\CustomerService;
use App\Service\Indent\OrderService;
use Illuminate\Http\Request;

class AuthorController extends Controller
{
    public function joinAuthorGroup(Request $request)
    {
        $uid = $request->uid;
        $introduce = $request->introduce;
        $phone = $request->phone;
        $weixin = $request->weixin;

        Result::isEmpty($phone,'手机不能为空',-27);
        Result::veryShort($introduce);
        if(!CustomerService::findUser(['uid'=>$uid])){
            Result::msg('-16',"error","无有效用户信息");
        }

        if(AuthorService::getAuthor($uid)){
            Result::msg('-28',"error","你已经是作者了");
        }

        if(AuthorService::getJoinLog($uid)){
            Result::msg('-29',"error","你已经申请过了");
        }

        $data = array(
            'uid' => $uid,
            'introduce' => $introduce,
            'phone' => $phone,
            'weixin' => $weixin,
            'created_at' => date("Y-m-d H:i:s")
        );

        if(!AuthorService::addJoinLog($data)){
            Result::msg('-11','error',"更新失败");
        }

        Result::msg('0','success',$data);
    }

    /**
     * 作者申请进度查询
     */
    public function joinAuthorStatus(Request $request)
    {
        $uid = $request->uid;

        $result = new Result();
        $is_author = CustomerService::findUser(['uid'=>$uid]);
        if($is_author->is_author){
            $result->status = 3;
        }else{
            $progress_log = AuthorService::getJoinLog($uid);
            $pay_status = OrderService::getOrderArray(['uid'=>$uid,'article_id'=>25819,'paystatus'=>1]);
            if($progress_log && $pay_status){
                //付了钱 有信息
                $result = $progress_log;

            }

            if($progress_log && !($pay_status)){
                //有信息没有付钱
                $result->status = 1;
            }

            if(!$progress_log && !($pay_status)){
                //没有申请
                $result->status = 0;
            }
        }

        
        Result::msg('0','success',$result);
    }


	/**
     *作者详情页
     */
    public  function authorDetailPage(Request $request)
    {
        Result::isEmpty($uid = $request->uid,"用户名不能为空");
        Result::isEmpty($auid = $request->auid,"用户名不能为空");

        if(!CustomerService::findUser(['uid'=>$uid])){
            Result::msg('-16',"error","无有效用户信息");
        }

        if(!CustomerService::findUser(['uid'=>$auid])){
            Result::msg('-16',"error","无有效用户信息");
        }
        
        $pages = AuthorService::getAuthorDetailPage($uid,$auid);
        Result::msg('0',"success",$pages);
    }

    /**
     * 个人主页
     */
    public  function myDetailPage(Request $request)
    {
        Result::isEmpty($request->uid,"用户名不能为空");
        if(!CustomerService::findUser(['uid'=>$request->uid])){
            Result::msg('-16',"error","无有效用户信息");
        }

        $pages = AuthorService::getMyDetailPage($request->uid);
        Result::msg('0',"success",$pages);
    }

    /**
     *  小程序个人主页
     */
    public  function myProgramDetailPage(Request $request)
    {
        Result::isEmpty($request->uid,"用户名不能为空");
        if(!CustomerService::findUser(['uid'=>$request->uid])){
            Result::msg('-16',"error","无有效用户信息");
        }

        $datas = AuthorService::getProgramMyDetailPage($request->uid);
        $datas = json_decode(json_encode($datas),true);
        $res = [
            'order0' => 0,
            'order1' => 0,
            'order2' => 0,
            'order3' => 0,
        ];
        foreach($datas as $data){
            switch ($data['order_status']){
                case 0:
                    $res['order0'] = $data['order_num'];
                    break;
                case 1:
                    $res['order1'] = $data['order_num'];
                    break;
                case 2:
                    $res['order2'] = $data['order_num'];
                    break;
                case 3:
                    $res['order3'] = $data['order_num'];
                    break;
            }
        }
        Result::msg('0',"success",$res);
    }
}